package com.txw.javaCore.server.controller;

import com.txw.javaCore.api.response.BaseResponse;
import com.txw.javaCore.api.response.StatusCode;
import com.txw.javaCore.model.entity.AppInfo;
import com.txw.javaCore.model.request.AppSearchRequest;
import com.txw.javaCore.server.common.SystemDataInit;
import com.txw.javaCore.server.service.AppService;
import com.txw.javaCore.server.service.RetryService;
import com.txw.javaCore.server.util.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 0100064695
 * @PROJECT_NAME: JavaCoreB2
 * @DESCRIPTION:
 * @USER: 涂玄武
 * @DATE: 2020/12/17 16:46
 */
@RestController
@RequestMapping(value = "app")
public class AppController extends BaseController {

    @Autowired
    private AppService appService;

    @Autowired
    private RetryService retryService;

    /**
     * 分页模糊查询
     *
     * @param appSearchRequest
     * @return
     */
    @GetMapping("search")
    public BaseResponse search(AppSearchRequest appSearchRequest) {
        log.info("--前端接受的请求参数：{}", appSearchRequest);

        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            //编写实际的业务代码
            //response.setData(appService.search(appSearchRequest));
            response.setData(appService.searchV2(appSearchRequest));
        } catch (Exception e) {
            log.error("app应用系统信息管理-分页模糊搜索-发生异常：{}", e.getMessage());
        }
        return response;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    public BaseResponse info(@PathVariable Integer id) {
        if (id == null || id <= 0) {
            return new BaseResponse(StatusCode.InvalidParams);
        }
        log.info("--前端接受的请求参数：{}", id);
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            //编写实际的业务代码
            //response.setData(appService.getInfo(id));
            //Retry重试机制
            response.setData(retryService.getAppInfo(id));
        } catch (Exception e) {
            log.error("app应用系统信息管理-获取详情-发生异常：{}", e.getMessage());
            response = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return response;
    }

    /**
     * 添加App版本(修改同理，只需加上id即可)
     *
     * @param info
     * @param result
     * @return
     */
    @PostMapping("add")
    public BaseResponse add(@RequestBody @Validated AppInfo info, BindingResult result) {
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(), checkRes);
        }
        log.info("--前端接受的请求参数：{}", info);
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            //问题：osType和device用户不方便输入，应维护在数据库字典中显示在下拉列表供用户选择
            response.setData(appService.add(info));

        } catch (Exception e) {
            log.error("app应用系统信息管理-添加-发生异常：{}", info, e);
            response = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return response;
    }

    /**
     * 若数据库数据发生变化，再次调用即可
     */
//    @Autowired
//    private SystemDataInit init;

    /**
     * 维护app应用信息所需要的数据字典
     *
     * @return
     */
    @RequestMapping(value = "data/dict")
    public BaseResponse appDataDict() {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            response.setData(appService.getAppDataDict());
//            init.initAppDataDict();
        } catch (Exception e) {
            log.error(" app应用系统信息管理-app应用所需要的的数据字典发生异常：{}", e.getMessage());
            response = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return response;
    }


    /**
     * 模板
     * @return
     */
    @RequestMapping(value = "test")
    public BaseResponse test() {
        log.info("--前端接受的请求参数：{}");
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {

        } catch (Exception e) {
            log.error("模块-功能发生异常：{}", e.getMessage());
            response = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return response;
    }
}
